/**
 * 静态路径解析器 - 兼容模式
 * 保留与原动态pathResolver相同的接口，但使用静态实现
 */

// 内联定义路径映射，避免依赖不存在的外部文件
// 项目路径映射 - 服务上下文
const SERVICE_PATHS = {
  // 根目录助手
  rootHelpers: {
    pageHelper: '../../../../helpers/page_helper.js',
    cloudHelper: '../../../../helpers/cloud_helper.js',
    timeHelper: '../../../../helpers/time_helper.js',
    validateHelper: '../../../../helpers/validate_helper.js',
    dataHelper: '../../../../helpers/data_helper.js',
    fileHelper: '../../../../helpers/file_helper.js',
    formHelper: '../../../../helpers/form_helper.js',
    helper: '../../../../helpers/helper.js',
    cacheHelper: '../../../../helpers/cache_helper.js'
  },
  
  // 全局配置
  configs: {
    helpersConfig: '../../../../config/helpers_config.js',
    libsConfig: '../../../../config/libs_config.js',
    modulesConfig: '../../../../config/modules_config.js'
  },
  
  // NPM包
  npm: {
    dayjs: '../../../../miniprogram_npm/dayjs/index',
    mobx: '../../../../miniprogram_npm/mobx/index',
    mobxBindings: '../../../../miniprogram_npm/mobx-miniprogram-bindings/index'
  }
};

// 项目路径映射 - 页面上下文
const PAGE_PATHS = {
  // 根目录助手
  rootHelpers: {
    pageHelper: '../../../../../helpers/page_helper.js',
    cloudHelper: '../../../../../helpers/cloud_helper.js',
    timeHelper: '../../../../../helpers/time_helper.js',
    validateHelper: '../../../../../helpers/validate_helper.js',
    dataHelper: '../../../../../helpers/data_helper.js',
    fileHelper: '../../../../../helpers/file_helper.js',
    formHelper: '../../../../../helpers/form_helper.js',
    helper: '../../../../../helpers/helper.js',
    cacheHelper: '../../../../../helpers/cache_helper.js'
  },
  
  // 全局配置
  configs: {
    helpersConfig: '../../../../../config/helpers_config.js',
    libsConfig: '../../../../../config/libs_config.js',
    modulesConfig: '../../../../../config/modules_config.js'
  },
  
  // NPM包
  npm: {
    dayjs: '../../../../../miniprogram_npm/dayjs/index',
    mobx: '../../../../../miniprogram_npm/mobx/index',
    mobxBindings: '../../../../../miniprogram_npm/mobx-miniprogram-bindings/index'
  }
};

/**
 * 静态路径解析器
 * 为保持兼容性，保留原有方法签名
 */
const pathResolver = {
  /**
   * 获取根目录辅助函数路径
   * @param {string} helperName 助手名称
   * @param {string} context 上下文（service或page）
   * @returns {string} 静态路径
   */
  getRootHelperPath(helperName, context = 'service') {
    // 移除可能的.js后缀，便于查找
    const baseName = helperName.replace(/.js$/, '');
    
    // 根据上下文选择路径映射
    const pathMap = context === 'page' ? PAGE_PATHS : SERVICE_PATHS;
    
    // 查找预定义的路径
    const helperKey = baseName.replace(/_helper$/, 'Helper');
    if (pathMap.rootHelpers[helperKey]) {
      return pathMap.rootHelpers[helperKey];
    }
    
    // 兜底方案：针对未预定义的助手使用标准路径
    console.warn(`未找到预定义的助手路径: ${helperName}，使用标准路径`);
    return context === 'page' 
      ? `../../../../../helpers/${helperName}` 
      : `../../../../helpers/${helperName}`;
  },
  
  /**
   * 获取项目辅助函数路径
   * @param {string} helperName 助手名称
   * @returns {string} 静态路径
   */
  getProjectHelperPath(helperName) {
    return `../helpers/${helperName}`;
  },
  
  /**
   * 获取NPM包路径
   * @param {string} packagePath 包路径
   * @param {string} context 上下文（service或page）
   * @returns {string} 静态路径
   */
  getNpmPath(packagePath, context = 'service') {
    // 根据上下文选择路径映射
    const pathMap = context === 'page' ? PAGE_PATHS : SERVICE_PATHS;
    
    // 尝试查找预定义的包路径
    const packageKey = packagePath.split('/')[0].replace(/-/g, '');
    if (pathMap.npm[packageKey]) {
      return pathMap.npm[packageKey];
    }
    
    // 兜底方案：使用标准路径
    return context === 'page'
      ? `../../../../../miniprogram_npm/${packagePath}`
      : `../../../../miniprogram_npm/${packagePath}`;
  },
  
  /**
   * 获取配置文件路径
   * @param {string} configName 配置名称
   * @param {string} context 上下文（service或page）
   * @returns {string} 静态路径
   */
  getConfigPath(configName, context = 'service') {
    // 移除可能的.js后缀，便于查找
    const baseName = configName.replace(/.js$/, '');
    
    // 根据上下文选择路径映射
    const pathMap = context === 'page' ? PAGE_PATHS : SERVICE_PATHS;
    
    // 查找预定义的配置路径
    const configKey = baseName.replace(/_config$/, 'Config');
    if (pathMap.configs[configKey]) {
      return pathMap.configs[configKey];
    }
    
    // 兜底方案：使用标准路径
    return context === 'page'
      ? `../../../../../config/${configName}`
      : `../../../../config/${configName}`;
  }
};

module.exports = pathResolver;